const util = require("../../../../utils/util");
const app = getApp();
const tmplIds = ["Us5cSgjk6oFqj6UxBMaob9pUbEKTXD4D15cyINwtCPM"];
let firstProjectIndex; // 两级联动第一列选择结果
let secondProjectIndex; // 两级联动第二列选择结果
let fatherProjects = []; // 全部父类数组
let sonProjects = []; // 父类对应的子类数组
let projectList = []; // 全部办事类型
const dateType = { "1": "工作日", "2": "自然日" }; // 办事日期类型映射关系
let rid, type, rdate; // 办事id、办事时间类型、时间间隔，请求使用
let toggleCollection = true;
Page({
    data: {
        projects: [],
        currentProject: [],
        date: "",
        projectAttr: { dateLeft: 20, timeType: "自然日" },
        needTime: "0",
        dateType: "工作日",
        scroll: false,
    },
    onLoad: function(options) {
        // util.colleation(this, "办事提醒日历");
        toggleCollection = true;
        util.getUrl(app.globalData.url).then(res => {
                // util.login().then(res => {
                // app.globalData.openid = res.data.openid;
                util.bstrequest('calendar.php', { ac: "type" }, "POST").then(res => {
                    projectList = JSON.parse(JSON.stringify(res.data.data));
                    fatherProjects = projectList.filter(v => v.pid === "0");
                    sonProjects = projectList.filter(v => v.pid === fatherProjects[0].id);
                    this.data.projects.push(fatherProjects);
                    this.data.projects.push(sonProjects);
                    rid = sonProjects[0].id;
                    type = sonProjects[0].type;
                    rdate = sonProjects[0].rdate;
                    this.setData({
                        projects: this.data.projects,
                        date: util.formatTime(new Date()).split(" ")[0].split("/").join("-"),
                        currentProject: [this.data.projects[0][0], this.data.projects[1][0], ],
                        needTime: sonProjects[0].rdate,
                        dateType: dateType[sonProjects[0].type],
                    });
                });
            })
            // });
    },
    // 办事项目
    changeProject(e) {
        firstProjectIndex = e.detail.value[0];
        secondProjectIndex = e.detail.value[1] ? e.detail.value[1] : 0;
        rid = this.data.projects[1][secondProjectIndex].id;
        type = this.data.projects[1][secondProjectIndex].type;
        rdate = this.data.projects[1][secondProjectIndex].rdate;
        this.setData({
            currentProject: [this.data.projects[0][firstProjectIndex], this.data.projects[1][secondProjectIndex], ],
            needTime: this.data.projects[1][secondProjectIndex].rdate,
            dateType: dateType[this.data.projects[1][secondProjectIndex].type],
        });
    },
    // 两级联动
    projectColumnChange(e) {
        let column = e.detail.column;
        let value = e.detail.value;
        if (column === 0)
            this.data.projects[1] = projectList.filter(v => v.pid === fatherProjects[value].id);
        this.setData({
            projects: this.data.projects,
        });
    },
    dateChange(e) {
        this.setData({
            date: e.detail.value,
        });
    },
    // 订阅消息
    addToRemind(e) {
        wx.requestSubscribeMessage({
            tmplIds,
            success: res => {
                if (res[`${tmplIds[0]}`] === "accept") {
                    // 接受订阅消息，发送请求
                    const $addRemind = {
                        ac: "addRemind",
                        openid: wx.getStorageSync("openid"),
                        rid,
                        rdate,
                        type,
                        starttime: this.data.date,
                    };
                    util.bstrequest('calendar.php', $addRemind, "POST").then(res => {
                        if (res.data.state === 1) {
                            wx.showModal({
                                title: '添加成功',
                                content: '添加办事提醒成功',
                                showCancel: false,
                                confirmText: '确定',
                                confirmColor: '#3CC51F',
                                success: (result) => {
                                    if (result.confirm)
                                        wx.navigateBack();
                                },
                            });
                        } else {
                            wx.showToast({
                                title: '添加失败，请稍后重试！',
                                icon: 'none',
                            });
                        }
                    });
                } else if (res[`${tmplIds[0]}`] === "reject") {
                    // 用户拒接接受订阅消息
                    wx.showToast({
                        title: '添加失败，请同意接受消息！',
                        icon: 'none',
                    });
                }
            }
        });
    },
    onShareAppMessage: function() {
        return {
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg"
        }
    },
    onPageScroll() {
        if (toggleCollection) {
            this.setData({
                scroll: true,
            });
            toggleCollection = false;
        }

    },
    closeCollection() {
        toggleCollection = false;
    },
    openCollection() {
        toggleCollection = true;
    }
})